<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/26
 * Time: 下午6:08
 */

namespace app\api\controller\v1;


use app\api\service\WgService;
use app\api\validate\WgAreaValidate;
use app\api\validate\WgDeviceValidate;
use think\Request;

class Wg extends UserAuth
{
    public function areaCreate()
    {
        (new WgAreaValidate())->goCheck('create');
        $data = Request::instance()->only(['name','address','area_head','org_id']);
        (new WgService())->areaCreate($this->guid,$data);
        return $this->showJsonReturnCode();
    }

    public function areaUpdate()
    {
        (new WgAreaValidate())->goCheck('update');
        $where = Request::instance()->only(['id']);
        $data = Request::instance()->only(['name','address','area_head','org_id']);
        (new WgService())->areaUpdate($this->guid,$where,$data);
        return $this->showJsonReturnCode();
    }

    public function areaGetList()
    {
//        (new WgAreaValidate())->goCheck('getList');
        $where = Request::instance()->only(['name']);
        $page = input('page');
        $orgRangeMap = ['org_id'=>['in',$this->orgRange]];
        $where = array_merge($where,$orgRangeMap);
        $result = (new WgService())->getWgAreaInfoListByMap($where,$page);
        return $this->showJsonReturnCode($result);
    }

    public function deviceCreate()
    {
        (new WgDeviceValidate())->goCheck('create');
        $data = Request::instance()->only(['code','guid','area_id','org_id']);
        (new WgService())->deviceCreate($this->guid,$data);
        return $this->showJsonReturnCode();
    }

    public function deviceUpdate()
    {
        (new WgDeviceValidate())->goCheck('update');
        $where = Request::instance()->only(['id']);
        $data = Request::instance()->only(['code','guid','area_id','org_id']);
        (new WgService())->deviceUpdate($this->guid,$where,$data);
        return $this->showJsonReturnCode();
    }

    public function deviceGetList()
    {
//        (new WgDeviceValidate())->goCheck('getList');
        $where = Request::instance()->only(['code','area_id']);
        $page = input('page');
        $orgRangeMap = ['org_id'=>['in',$this->orgRange]];
        $where = array_merge($where,$orgRangeMap);
        $result = (new WgService())->getWgDeviceInfoListByMap($where,$page);
        return $this->showJsonReturnCode($result);
    }
}